package com.flex.core.model;

import java.util.HashMap;
import java.util.Map;

import com.flex.core.dao.Page;
import com.flex.core.dao.PageInfo;

/**
 * 前台js与后台通讯，所封装好的json包
 * 
 * @author radio
 * 
 */
public class JsonResponse {

	private ErrorInfo error;

	private Map<String,Object> data;
	
	private PageInfo page;

	public JsonResponse() {
		this.error = new ErrorInfo();
		this.error.setCode(0);
		this.error.setMsg("操作成功");
	}
	
	public ErrorInfo getError() {
		return error;
	}

	public void setError(ErrorInfo error) {
		this.error = error;
	}

	public PageInfo getPage() {
		return page;
	}

	public void setPage(PageInfo page) {
		this.page = page;
	}

	/**
	 * 设置错误信息
	 * @param code
	 * @param msg
	 */
	public void setErrorInfo(int code, String msg) {
		this.error = new ErrorInfo();
		this.error.setCode(code);
		this.error.setMsg(msg);
	}

	/**
	 * 从dao的分页查询方法返回的page对象中得到分页信息
	 * @param page
	 */
	public void setPageInfo(Page page) {
		this.page = new PageInfo();
		this.page.setPageNo(page.getCurrentPageNo());
		this.page.setPageSize(page.getPageSize());
		this.page.setTotalCount(page.getTotalCount());
		this.page.setTotalPage(page.getTotalPageCount());
		this.page.setHasNextPage(page.hasNextPage());
		this.page.setHasPreviousPage(page.hasPreviousPage());
	}

	public Map<String, Object> getData() {
		if (data == null) {
			data = new HashMap<String, Object>();
		}
		return data;
	}

}
